package cn.com.ht.security.security.service;

import cn.com.ht.security.entity.User;
import cn.com.ht.security.security.JWTUser;
import cn.com.ht.security.security.JWTUserFactory;
import cn.com.ht.security.security.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class JWTUserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserRepository repository;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = repository.findByUsername(username);
        if (null == user){
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.",username));
        }else {
            return JWTUserFactory.create(user);
        }

    }






}
